<?php
namespace Client\Controller;

use Org\Net\Http;
use Think\Controller;
class BaiduLBSController extends Controller {
	public function index(){
		$http = new Http();
		$url = C('BAIDU_LIST_GEOTABLE')."?ak=".C('GSBAIDU_AK');
		echo $http->fsockopenDownload($url);
	}
	public function getsearch(){
		$http = new Http();
		$location = '120.696615,28.010912';
		$url = C('BAIDU_GEOSEARCH')."?ak=".C('BAIDU_AK')."&sortby=distance:1&geotable_id=103620&location={$location}&page_size=1";
		echo $http->fsockopenDownload($url);
	}
	public function getLocal(){
		$http = new Http();
		$geotable_id ='111203';
		$url = C('BAIDU_LOCAL')."?ak=".C('BAIDU_AK')."&geotable_id={$geotable_id}&page_size=10&region={$_GET['region']}";
		$back =  $http->fsockopenDownload($url);
		var_dump(json_decode($back,'json'));
	}
	public function getScenicPoint(){
		$http = new Http();
		$tags = "MM";
		$location = '120.70402250551,28.012121059421';
		$url = C('BAIDU_GEOSEARCH')."?ak=".C('BAIDU_AK')."&sortby=distance:1&geotable_id=103620&location={$location}";
		echo $http->fsockopenDownload($url);
	}
	public function addPoi(){
		if(IS_POST){
			$types = array('Ios','Android');
			$geotables = array(
				'Ios'=>'105083',
				'Android'=>'105084'
				);
				$coord_types = array(
					'Ios'=>1,
					'Android'=>2
				);
				$type = $_POST['type'];
				if(in_array_case($type, $types)){
					$http = new Http();
					$post['latitude'] = $_POST['latitude'];
					$post['longitude'] = $_POST['longitude'];
					$post['geotable_id'] = $geotables[$_POST['type']];
					$post['ak'] = C("BAIDU_AK");
					$post['coord_type'] = $coord_types[$_POST['type']];
					$post['title'] = $_POST['name'];
					$post['tags'] = $_POST['belong'];
					$data['post'] = $post;
					$back = $http->fsockopenDownload(C("BAIDU_POI_CREATE"),$data);
					//					file_put_contents("Public/Log/log.txt", "back:".$back."\r\n",FILE_APPEND|LOCK_EX);
					$back = json_decode($back);
					if($back->status == 0){
						$json['result'] = true;
						$json['err'] = $back->message;
					}else{
						$json['result'] = false;
						$json['err'] = '查询失败：没有该类型定点';
					}
				}else{
					$json['result'] = false;
					$json['err'] = '查询失败：未知类型';
				}
				$this->ajaxReturn($json);
		}
	}
}